#include "network.h"
#include <string.h>
#include <stdio.h>

//http(s)配置
MODULE_ERRPO module_https_config(ATModuleConfig* cfg)
{
    if(cfg)
    {
        AT_ERROR err;
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                "AT+QHTTPCFG=\"contextid\",1\r\n",
                                strlen("AT+QHTTPCFG=\"contextid\",1\r\n"),
                                "OK",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        
        err = at_parser_send_wait(&cfg->atConfig,
                                "AT+QHTTPCFG=\"requestheader\",0\r\n",
                                strlen("AT+QHTTPCFG=\"requestheader\",0\r\n"),
                                "OK",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        
        err = at_parser_send_wait(&cfg->atConfig,
                                "AT+QHTTPCFG=\"responseheader\",0\r\n",
                                strlen("AT+QHTTPCFG=\"responseheader\",0\r\n"),
                                "OK",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        
        if(err == AT_ERR_NONE)
            return MODULE_ERR_NONE;
    }
    return MODULE_ERR_PARAM;
}

//http(s)连接
MODULE_ERRPO module_https_connect(ATModuleConfig* cfg,char* url)
{
    if(cfg)
    {
        AT_ERROR err;
        char httpUrlBuff[300];
        int urlLen = strlen(url);
        snprintf(httpUrlBuff,300,"AT+QHTTPURL=%d\r\n",urlLen);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                httpUrlBuff,
                                strlen(httpUrlBuff),
                                "CONNECT",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        
        err = at_parser_send_wait(&cfg->atConfig,
                                url,
                                urlLen,
                                "OK",
                                0,
                                60*1000,
                                1
                                );
        
        if(err == AT_ERR_NONE)
            return MODULE_ERR_NONE;
    }
    return MODULE_ERR_PARAM;
}

//http(s)发送
MODULE_ERRPO module_https_post(ATModuleConfig* cfg,unsigned char* buff,int len)
{
    if(cfg)
    {
        AT_ERROR err;
        char httpPostBuff[128];
        snprintf(httpPostBuff,128,"AT+QHTTPPOST=%d,80\r\n",len);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                httpPostBuff,
                                strlen(httpPostBuff),
                                "CONNECT",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        
        err = at_parser_send_wait(&cfg->atConfig,
                                (char*)buff,
                                len,
                                "+QHTTPPOST: 0,200",
                                0,
                                60*1000,
                                1
                                );
        
        if(err == AT_ERR_NONE)
            return MODULE_ERR_NONE;
    }
    return MODULE_ERR_PARAM;
}

//http(s) 获取(下载)
MODULE_ERRPO module_https_get_to_file(ATModuleConfig* cfg,const char* filename,int timeout,int *contentLen)
{
    if(cfg)
    {
        AT_ERROR err;
        char httpGetBuff[128];
        const char* recBuf;
        const char* resultHead = 0;
        snprintf(httpGetBuff,128,"AT+QHTTPGET=%d\r\n",timeout);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                httpGetBuff,
                                strlen(httpGetBuff),
                                "+QHTTPGET: 0,200",
                                &recBuf,
                                timeout*1000,
                                1
                                );
        resultHead = strstr(recBuf,"+QHTTPGET:");
        if(resultHead)
        {
            int a,b;
            sscanf(resultHead,"+QHTTPGET: %d,%d,%d",&a,&b,contentLen);
        }
        
        snprintf(httpGetBuff,128,"AT+QHTTPREADFILE=\"UFS:%s\",%d\r\n",filename,timeout);
        err = at_parser_send_wait(&cfg->atConfig,
                                httpGetBuff,
                                strlen(httpGetBuff),
                                "+QHTTPREADFILE: 0",
                                0,
                                timeout*1000,
                                1
                                );
        
        if(err == AT_ERR_NONE)
            return MODULE_ERR_NONE;
    }
    return MODULE_ERR_PARAM;
}
